import {
  imageLiveList,
  videoLiveList
} from '../../apis/live';
import {
  splicing,
  liveStartTime
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    height: wx.getMenuButtonBoundingClientRect().height + wx.getMenuButtonBoundingClientRect().top + 6,
    navIndex: 0,
    vdLiveList: [],
    vdPageNum: 1,
    vdHaveMore: true,
    imgLiveList: [],
    imgPageNum: 1,
    imgHaveMore: true,
    isRequest: false,
    isRefresh: false
  },

  onLoad() {
    this.getVideoLiveList(1);
  },

  onShareAppMessage() {
    return {
      title: '北京市移民及出入境服务行业协会',
      path: '/pages/start/index',
      imageUrl: '/images/logo.png'
    }
  },

  // scroll-view触底
  bindscrolltolower() {
    if (this.data.navIndex === 0) {
      if (this.data.vdHaveMore && !this.data.isRequest) {
        this.getVideoLiveList(this.data.imgPageNum + 1, 'load');
      };
    } else {
      if (this.data.imgHaveMore && !this.data.isRequest) {
        this.getImageLiveList(this.data.imgPageNum + 1, 'load');
      };
    };
  },
  // scroll-view下拉
  bindrefresherrefresh() {
    if (this.data.navIndex === 0) {
      this.getVideoLiveList(1);
    } else {
      this.getImageLiveList(1);
    };
  },

  // 获取视频直播列表
  getVideoLiveList(pageNum, type) {
    if (this.data.isRequest) return
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    this.setData({
      isRequest: true
    });
    videoLiveList(pageNum, 10).then(res => {
      wx.hideLoading();
      this.setData({
        isRequest: false,
        isRefresh: false
      });
      if (res.data.code === 200) {
        this.setData({
          vdPageNum: pageNum
        });
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          v.coverImg = splicing(v.coverImg);
          v.startTime = v.startTimeView;
          v.startTimeView = liveStartTime(v.startTimeView);
        });
        if (type === 'load') {
          rows.forEach(v => {
            this.data.vdLiveList.push(v);
          });
          this.setData({
            vdLiveList: this.data.vdLiveList
          });
        } else {
          this.setData({
            vdLiveList: rows
          });
        };

        if (this.data.vdLiveList.length === total) {
          this.setData({
            vdHaveMore: false
          });
        };
      };
    });
  },
  // 获取图片直播列表
  getImageLiveList(pageNum, type) {
    if (this.data.isRequest) return
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    this.setData({
      isRequest: true
    });
    imageLiveList(pageNum, 10).then(res => {
      wx.hideLoading();
      this.setData({
        isRequest: false,
        isRefresh: false
      });
      if (res.data.code === 200) {
        this.setData({
          imgPageNum: pageNum
        });
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          v.coverImg = splicing(v.coverImg);
          v.startTimeView = liveStartTime(v.startTimeView);
        });
        if (type === 'load') {
          rows.forEach(v => {
            this.data.imgLiveList.push(v);
          });
          this.setData({
            imgLiveList: this.data.imgLiveList
          });
        } else {
          this.setData({
            imgLiveList: rows
          });
        };

        if (this.data.imgLiveList.length === total) {
          this.setData({
            imgHaveMore: false
          });
        };
      };
    });
  },

  // 切换直播
  changeNav(e) {
    let {
      index
    } = e.currentTarget.dataset;
    if (index === 0 && this.data.vdLiveList.length === 0) {
      this.getVideoLiveList(1);
    } else if (index === 1 && this.data.imgLiveList.length === 0) {
      this.getImageLiveList(1);
    };
    this.setData({
      navIndex: index
    });
  },

  // 直播详情
  liveDetails(e) {
    console.log("直播详情")
    let {
      id,itemlink
    } = e.currentTarget.dataset;
   if(itemlink.link==2 && itemlink.linkUrl != null){
    wx.navigateTo({
      url: `/pages/liveWeb/liveWeb`
    });
   }else{
    wx.navigateTo({
      url: `/pages/liveDetails/index?liveId=${id}`
    });
   }
    
  },

  // 观看直播
  watchLive(e) {
    let {
      index
    } = e.currentTarget.dataset;
    let live = this.data.vdLiveList[index];
    if (live.liveStatus === 101) {
      let roomId = [live.wxRoomId];
      // let customParams = encodeURIComponent(JSON.stringify({
      //   path: 'pages/index/index',
      //   pid: 1
      // }));
      wx.navigateTo({
        url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}`
      });
    } else if (live.liveStatus === 103 && live.closeReplay === 0) {
      if (live.replay) {
        wx.navigateTo({
          url: `/pages/livePlayBack/index?name=${live.name}&startTime=${live.startTime}&closeTime=${live.endTimeView}&replay=${live.replay}`
        });
      } else {
        wx.showToast({
          title: '回放生成中...',
          duration: 1500,
          icon: 'none'
        });
      };
    };
  }
})